package com.nb.cloud.doc.biz.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nb.cloud.doc.entity.SysKeyword;
import com.nb.cloud.doc.entity.SysKeywordExtend;
import com.nb.cloud.doc.entity.TextbookVersion;
import com.nb.cloud.doc.enums.ExtKeywordTypeEnum;
import com.nb.cloud.doc.enums.GradeEnum;
import com.nb.cloud.doc.enums.RulesEnum;
import com.nb.cloud.doc.enums.TermEnum;
import com.nb.cloud.doc.model.vo.SuggestInitVO;
import com.nb.cloud.doc.model.vo.SuggestVO;
import com.nb.cloud.doc.biz.ISuggestBiz;
import com.nb.cloud.doc.service.ISysKeywordExtendService;
import com.nb.cloud.doc.service.ISysKeywordService;
import com.nb.cloud.doc.service.ITextbookVersionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lihaoyang6
 * @date 2024年08月04日 11:54
 */
@Slf4j
@Service
public class SuggestBizImpl implements ISuggestBiz {



    @Autowired
    RestHighLevelClient restHighLevelClient;

    //搜索建议的索引
    public static final String suggestIndex = "cloud_doc_suggest";
    //搜索建议名称
    public static final String suggest_name = "keyword_suggest";




    @Override
    public List<SuggestVO> searchSuggest(String keyword) {

        List<SuggestVO> suggestVOList = new ArrayList<>();

        SearchRequest searchRequest = new SearchRequest(suggestIndex);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        SuggestBuilder suggestBuilder = new SuggestBuilder();
        //completion类型搜索
        CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion("keyword")
                .prefix(keyword).size(10);//最多返回10条
        //添加搜索建议
        suggestBuilder.addSuggestion(suggest_name, completionSuggestionBuilder);

        //设置建议搜索
        searchSourceBuilder.suggest(suggestBuilder);
        //设置查询请求
        searchRequest.source(searchSourceBuilder);
        //查询
        try {
            SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            Suggest suggest = response.getSuggest();
            CompletionSuggestion suggestion = suggest.getSuggestion(suggest_name);
            List<CompletionSuggestion.Entry.Option> options = suggestion.getOptions();
            for (CompletionSuggestion.Entry.Option option : options) {
                Map<String, Object> sourceMap = option.getHit().getSourceAsMap();
                System.out.println(sourceMap);
                String id = (String) sourceMap.get("id");
                String showName = (String) sourceMap.get("sysKeywordName");
                String type = (String) sourceMap.get("sysKeywordType");
                SuggestVO suggestVO = SuggestVO.builder()
                        .id(id)
                        .showName(showName)
                        .type(type).build();
                suggestVOList.add(suggestVO);
            }
        } catch (Exception e) {
            log.error("搜索出错", e);
        }
        return suggestVOList;
    }
}
